package com.example.cloudecg;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DataTools {
    private float getFloat(byte[] data) {
        int accum = 0;
        accum = accum | (data[0] & 0xff) << 24;
        accum = accum | (data[1] & 0xff) << 16;
        accum = accum | (data[2] & 0xff) << 8;
        accum = accum | (data[3] & 0xff) << 0;
        return Float.intBitsToFloat(accum);
    }

    private int ConvertToInt(byte[] b) {
        return b[0] & 0xFF |
                (b[1] & 0xFF) << 8 |
                (b[2] & 0xFF) << 16 |
                (b[3] & 0xFF) << 24;
    }

    private short ConvertToShort(byte[] data, int position) {
        return (short) (((data[position + 1] & 0xff) << 8) | (data[position] & 0xff));
    }

    public static int decodeHEX(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        int numb = bigint.intValue();

        return numb;
    }

    public static String readData(byte[] data) {
        if (data.length > 9) {
            byte[] dateValue2 = {data[9], data[10], data[11], data[12]}; // datetime
            byte[] feValue2 = {data[13], data[14]}; // PEF Value
            byte[] pefValue2 = {data[15], data[16]}; // FEV1 Value
            // 逆置字节序
            byte[] revDate2 = bytesReverseOrder(dateValue2);
            byte[] revPef2 = bytesReverseOrder(pefValue2);
            byte[] revFev2 = bytesReverseOrder(feValue2);

            long date2 = Long.valueOf(bytesToHexString(revDate2), 16);

            String time = GtmToStd(date2);

            int pef2 = Integer.valueOf(bytesToHexString(revPef2), 16);
            float fev2 = (float) (Integer.valueOf(bytesToHexString(revFev2), 16)) / 100;

            return "Date:" + time + " " + "PEF:" + pef2 + " " + "FEV:" + fev2;
        } else {
            byte[] dateValue1 = {data[4], data[3], data[2], data[1]}; // datetime		Value
            byte[] feValue1 = {data[6], data[5]}; // PEF Value
            byte[] pefValue1 = {data[8], data[7]}; // FEV1 Value

            // 格林威治时间
            long date1 = Long.valueOf(bytesToHexString(dateValue1), 16);

            String time = GtmToStd(date1);

            int pef1 = Integer.valueOf(bytesToHexString(pefValue1), 16);
            float fev1 = (float) (Integer.valueOf(bytesToHexString(feValue1), 16)) / 100;

            return "Date:" + time + " " + "PEF:" + pef1 + " " + "FEV:" + fev1;
        }
    }

    public static String GtmToStd(Long gtm) {
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.SIMPLIFIED_CHINESE);
        String datetime = sdf.format(gtm * 1000);
        return datetime;*/
        //由于底层设备记录时间的格式决定了采取GMT时区的标准时间
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sd.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sd.format(new Date(gtm * 1000));
    }

    public  static byte[] bytesReverseOrder(byte[] b) {
        int length = b.length;
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            result[length - i - 1] = b[i];
        }
        return result;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String formatHexString(byte[] data, boolean addSpace) {
        if (data == null || data.length < 1)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
            if (addSpace)
                sb.append(" ");
        }
        return sb.toString().replace(" ", "");
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }





}
